package com.mall.goods.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.goods.mapper.PmsProductCategoryMapper;
import com.mall.model.domain.PmsProductCategory;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.PmsProductCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class PmsProductCategoryService extends ServiceImpl<PmsProductCategoryMapper, PmsProductCategory> {

    @Autowired(required = false)
    private PmsProductCategoryMapper pmsProductCategoryMapper;

    /**
     * 根据品牌名称分页获取品牌列表
     *
     * @return
     */
    public List listWithChildren() {
        //从表中查询到pmsProductCategories数据
        List<PmsProductCategory> pmsProductCategories = list();
        //把PmsProductCategory的集合pmsProductCategories包装成PmsProductCategoryVO集合
        List<PmsProductCategoryVO> PmsProductCategoryVOs = pmsProductCategories.stream().map(pmsProductCategory -> {
            PmsProductCategoryVO pmsProductCategoryVO = new PmsProductCategoryVO();
            BeanUtils.copyProperties(pmsProductCategory, pmsProductCategoryVO);
            return pmsProductCategoryVO;
        }).collect(Collectors.toList());

        //filter是java8的写法，取得所有parentId为0的数据，也就是一级目录
        //map也是java8的写法，用于封装数据，取得他的孩子（也就是下级目录）的数据
        List<PmsProductCategoryVO> list = PmsProductCategoryVOs.stream().filter(PmsProductCategoryVO ->
                PmsProductCategoryVO.getParentId() == 0
        ).map((menu) -> {
            menu.setChildren(getChildrenData(menu, PmsProductCategoryVOs));
            return menu;
        }).collect(Collectors.toList());
        return list;
    }

    //获取孩子（下级目录）的方法，递归实现
    private List getChildrenData(PmsProductCategoryVO root, List<PmsProductCategoryVO> all) {
        List<PmsProductCategoryVO> children = all.stream().filter(pmsProductCategoryVO ->
                Objects.equals(pmsProductCategoryVO.getParentId(), root.getId())
        ).map(pmsProductCategoryVO -> {
            pmsProductCategoryVO.setChildren(getChildrenData(pmsProductCategoryVO, all));
            return pmsProductCategoryVO;
        }).collect(Collectors.toList());
        return children;
    }


    /**
     * 分页查询商品分类
     *
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public CommonPage listPage(Integer parentId, Integer pageNum, Integer pageSize) {
        Page<PmsProductCategory> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件
        queryWrapper.eq(parentId != null, PmsProductCategory::getParentId, parentId);
        Integer count = pmsProductCategoryMapper.selectCount(queryWrapper);
        pmsProductCategoryMapper.selectPage(pageInfo, queryWrapper);
        return new CommonPage(pageNum, pageSize, count, pageInfo.getRecords());
    }
}
